﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;

namespace PeisongWeb
{
    public partial class customerUpdatePwdList : adminPage
    {
        protected int companyId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["companyId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["companyId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["company"] != null)
                {
                    if (Library.Number.IsIntPositive(Request.QueryString["company"]))
                    {
                        companyId = Convert.ToInt32(Request.QueryString["company"]);
                        lbl_Company.Text = logic.company.getById(companyId).Company;
                    }
                }
                rptBind();
            }
        }
        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("companyId=" + companyId);
            if (txt_username.Text.Trim() != "")
                where.AppendFormat("and username='{0}'", txt_username.Text.Trim());
            if (txt_realname.Text.Trim() != "")
                where.AppendFormat("and realname='{0}'", txt_realname.Text.Trim());
            if (txt_mobile.Text.Trim() != "")
                where.AppendFormat("and phone='{0}'", txt_mobile.Text.Trim());
            DataSet ds = logic.customer.select(where.ToString(), "customerId desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Update")
            {
                int customerId = Convert.ToInt32(e.CommandArgument);
                TextBox txtpwd = (TextBox)e.Item.FindControl("txt_pwd");
                TextBox txtphone = (TextBox)e.Item.FindControl("txt_phone");
                CheckBox chksmg = (CheckBox)e.Item.FindControl("chkSMG");
                if (txtpwd.Text.Length > 0)
                {
                    if (chksmg.Checked)
                    {
                        if (Regex.IsMatch(txtphone.Text, @"^13[0-9]{1}[0-9]{8}|^15[0-3]{1}[0-9]{8}|^15[5-9]{1}[0-9]{8}|^18[0]{1}[0-9]{8}|^18[5-9]{1}[0-9]{8}$", RegexOptions.IgnoreCase))
                        {
                            logic.customer.updatePwdByadmin(customerId, Library.StringLbl.EncryptMD5(txtpwd.Text),logic.sysAdmin.AdminID,GetClientIP());
                            string content = "您的账号密码已修改为" + txtpwd.Text;
                            logic.sysAdmin.SendSMS(txtphone.Text, content, customerId, Request.Url.ToString());
                            Library.Script.ClientMsg("密码修改成功!");
                        }
                        else
                        {
                            Library.Script.ClientMsg("手机号码不正确！");
                        }
                    }
                    else
                    {
                        logic.customer.updatePwdByadmin(customerId, Library.StringLbl.EncryptMD5(txtpwd.Text), customerId, GetClientIP());
                        Library.Script.ClientMsg("密码修改成功!");
                    }
                }
                else
                {
                    Library.Script.ClientMsg("密码不能为空！");
                }
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            rptBind();
        }
    }
}
